package sz.szsmk.citizencard.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.Toast;


import java.util.List;

import sz.szsmk.citizencard.model.network.bean.response.ShopListRes;
import sz.szsmk.citizencard.util.zxyh.CoordinateChange;

public class Utils {

    //屏幕宽度（像素）
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 判断本地是否安装某一个应用
     *
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isAvailable(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        //获取手机系统的所有APP包名，然后进行一一比较
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        for (int i = 0; i < pinfo.size(); i++) {
            if (((PackageInfo) pinfo.get(i)).packageName
                    .equalsIgnoreCase(packageName))
                return true;
        }
        return false;
    }

    public static void startMapBylocation(Context mContext, double latitude, double longitude, String infoWindowContent) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        if (Utils.isAvailable(mContext, "com.baidu.BaiduMap")) {
            intent.setData(Uri.parse("baidumap://map/marker?location=" + latitude + "," + longitude + "&title=" + infoWindowContent + "&traffic=on"));
            mContext.startActivity(intent);
        } else if (Utils.isAvailable(mContext, "com.autonavi.minimap")) {
            Uri uri = Uri.parse("androidamap://viewMap?sourceApplication=启业云&poiname=" + infoWindowContent + "&lat=" + latitude + "&lon=" + longitude + "&dev=0");
            intent.setData(uri);
            mContext.startActivity(intent);
        } else if (Utils.isAvailable(mContext, "com.tencent.map")) {
//                qqmap://map/marker?coord_type=1&marker=coord:39.892326,116.342763;title:超好吃冰激凌;addr:手帕口桥北铁路道口
            intent.setData(Uri.parse("qqmap://map/marker?marker=coord:" + latitude + "," + longitude + ";title:" + infoWindowContent + "&referer=启业云"));
            mContext.startActivity(intent);
        } else {
            Toast.makeText(mContext, "地图应用未安装", Toast.LENGTH_SHORT).show();
        }
    }
    //传入的经纬度参数是百度坐标的参数，如果打开高德地图或者其它地图需要进行坐标转换
    public static void startMapBylocationByBaiduCorSys(Context mContext, double latitude, double longitude, String infoWindowContent) {
    }
    /**
     * 展示地图相关
     * 判断顺序 百度地图 -> 高德地图 -> 腾讯地图
     *
     * @param infosBean
     */
    public static void startMap(Context mContext, ShopListRes.ShopListInfosBean infosBean) {
//        Object latitude1 = infosBean.getLatitude();
//        if (latitude1 == null){
//            return;
//        }
        double latitude = (double) infosBean.getLatitude();
        double longitude = (double) infosBean.getLongitude();
        String infoWindowContent = infosBean.getShopName();//提示框内容

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);

        if (Utils.isAvailable(mContext, "com.baidu.BaiduMap")) {
            intent.setData(Uri.parse("baidumap://map/marker?location=" + latitude + "," + longitude + "&title=" + infoWindowContent + "&traffic=on"));
            mContext.startActivity(intent);
        }else{
            Toast.makeText(mContext, "请先安装百度地图", Toast.LENGTH_SHORT).show();
        }

//        if (Utils.isAvailable(mContext, "com.baidu.BaiduMap")) {
//            intent.setData(Uri.parse("baidumap://map/marker?location=" + latitude + "," + longitude + "&title=" + infoWindowContent + "&traffic=on"));
//            mContext.startActivity(intent);
//        } else if (Utils.isAvailable(mContext, "com.autonavi.minimap")) {
//            Uri uri = Uri.parse("androidamap://viewMap?sourceApplication=启业云&poiname=" + infoWindowContent + "&lat=" + latitude + "&lon=" + longitude + "&dev=0");
//            intent.setData(uri);
//            mContext.startActivity(intent);
//        } else if (Utils.isAvailable(mContext, "com.tencent.map")) {
////                qqmap://map/marker?coord_type=1&marker=coord:39.892326,116.342763;title:超好吃冰激凌;addr:手帕口桥北铁路道口
//            intent.setData(Uri.parse("qqmap://map/marker?marker=coord:" + latitude + "," + longitude + ";title:" + infoWindowContent + "&referer=启业云"));
//            mContext.startActivity(intent);
//        } else {
//            Toast.makeText(mContext, "地图应用未安装", Toast.LENGTH_SHORT).show();
//        }
    }

    public static void startSearchMap(Context mContext, double latitude, double longitude, String infoWindowContent) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        if (Utils.isAvailable(mContext, "com.baidu.BaiduMap")) {
            intent.setData(Uri.parse("baidumap://map/marker?location=" + latitude + "," + longitude + "&title=" + infoWindowContent + "&traffic=on"));
            mContext.startActivity(intent);
        } else if (Utils.isAvailable(mContext, "com.autonavi.minimap")) {
            Uri uri = Uri.parse("androidamap://viewMap?sourceApplication=启业云&poiname=" + infoWindowContent + "&lat=" + latitude + "&lon=" + longitude + "&dev=0");
            intent.setData(uri);
            mContext.startActivity(intent);
        } else if (Utils.isAvailable(mContext, "com.tencent.map")) {
//                qqmap://map/marker?coord_type=1&marker=coord:39.892326,116.342763;title:超好吃冰激凌;addr:手帕口桥北铁路道口
            intent.setData(Uri.parse("qqmap://map/marker?marker=coord:" + latitude + "," + longitude + ";title:" + infoWindowContent + "&referer=启业云"));
            mContext.startActivity(intent);
        } else {
            Toast.makeText(mContext, "地图应用未安装", Toast.LENGTH_SHORT).show();
        }
    }
}
